package com.ljf.tijian.AI;

import com.ljf.tijian.config.HttpSessionConfig;
import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import java.io.IOException;

@CrossOrigin
@Component
@ServerEndpoint(value = "/chat/{id}",configurator = HttpSessionConfig.class)
public class WebSocketAI{


    private static KimiAiService kimiAiService ;

    private static ApplicationContext applicationContext;

    public static void setApplicationContext(ApplicationContext applicationContext){
        WebSocketAI.applicationContext = applicationContext;
    }

    @OnOpen
    public void onOpen(@PathParam("id") final long id, Session session) throws IOException {
        System.out.println(id);
//        ollama = applicationContext.getBean(LocalDeepSeekServiceImpl.class);
        kimiAiService = applicationContext.getBean(KimiAiService.class);
    }

    @OnMessage
    public void onmessage(String message,Session session) throws EncodeException, IOException {
//        if(message.contains("套餐"))
//            session.getBasicRemote().sendText(ollama.call("套餐包含:基础套餐。。。"+message));
//        else
        session.getBasicRemote().sendText(kimiAiService.call(message));

    }

    @OnClose
    public void close(){
        System.out.println("websocket close");
    }

    @OnError
    public void onError(Throwable throwable,Session session){

        throwable.printStackTrace();
    }
}
